package com.front.managementfront.driver.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.front.managementfront.driver.entity.CarInfo;
import com.front.managementfront.driver.entity.CertificateImg;
import com.front.managementfront.driver.mapper.CarInfoMapper;
import com.front.managementfront.driver.mapper.CertificateImgMapper;
import com.front.managementfront.driver.service.ICarInfoService;
import com.front.managementfront.driver.vo.CarInfoListVo;
import com.front.managementfront.driver.vo.UserVo;
import com.front.managementfront.fegin.IUserAppServiceClient;
import com.front.managementfront.utils.PhotoUtils;
import com.hkts.management.common.utils.BeanConvertUtils;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.vo.UserBasicInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-11-19
 */
@Service
@Transactional
public class CarInfoServiceImpl extends ServiceImpl<CarInfoMapper, CarInfo> implements ICarInfoService {


    @Autowired
    private CarInfoMapper carInfoMapper;
    @Autowired
    private CertificateImgMapper certificateImgMapper;

    @Autowired
    private IUserAppServiceClient iUserAppServiceClient;
    @Override
    public IPage<CarInfo> selectPage(Page<CarInfo> page, CarInfo carInfo) {
        QueryWrapper<CarInfo> query = Wrappers.query();
        QueryWrapper<CarInfo> entityWrapper = getEntityWrapper(query, carInfo);
        return carInfoMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<CarInfo> selectList(CarInfo carInfo) {
        QueryWrapper<CarInfo> query = Wrappers.query();
        QueryWrapper<CarInfo> entityWrapper = getEntityWrapper(query, carInfo);
        return carInfoMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<CarInfo> getEntityWrapper(QueryWrapper<CarInfo> entityWrapper, CarInfo carInfo) {
        //条件拼接

        return entityWrapper;
    }

    @Override
    public CarInfoListVo getCarInfoByUserId(String userId) {
        CarInfoListVo vo = new CarInfoListVo();
        QueryWrapper<CarInfo> query = Wrappers.query();
        query.lambda().eq(CarInfo::getUserId, userId);
        query.lambda().eq(CarInfo::getDelFlag, 0);
        query.lambda().ne(CarInfo::getApprovalStatus,0);
        List<CarInfo> carInfos = carInfoMapper.selectList(query);
        List<String> collectApprovalUser=new ArrayList<>();
        for(int i=0;i<carInfos.size();i++){
            if(carInfos.get(i).getApprovalUser()!=null&&!collectApprovalUser.contains(carInfos.get(i).getApprovalUser())&&!carInfos.get(i).getApprovalUser().equals("admin")){
                collectApprovalUser.add(carInfos.get(i).getApprovalUser());
            }
        }
        if(!collectApprovalUser.isEmpty()){
            Result<Map<String, Object>> usersByuserIds = iUserAppServiceClient.getUsersByuserIds(collectApprovalUser);
            for(int i=0;i<carInfos.size();i++){
                if(carInfos.get(i).getApprovalUser()!=null&&!carInfos.get(i).getApprovalUser().equals("admin")){
                    UserVo userVo = BeanConvertUtils.convertBean(usersByuserIds.getResult().get(carInfos.get(i).getApprovalUser()), UserVo.class);
                    carInfos.get(i).setApprovalUser(userVo.getNickName());
                }
            }
        }
        QueryWrapper<CertificateImg> queryCertificateImg = Wrappers.query();
        queryCertificateImg.lambda().eq(CertificateImg::getUserId, userId);
        List<CertificateImg> certificateImgs = certificateImgMapper.selectList(queryCertificateImg);
        for(int i=0;i<certificateImgs.size();i++){
            certificateImgs.get(i).setCardPath(PhotoUtils.handlerPtotoUrlPrefix(certificateImgs.get(i).getCardPath()));
        }
        for (int i = 0; i < carInfos.size(); i++) {
            String id = carInfos.get(i).getId();
            List<CertificateImg> collect = certificateImgs.stream().filter(certificateImg -> id.equals(certificateImg.getCarId())).collect(Collectors.toList());
            Map<String, CertificateImg> collect1 = collect.stream().collect(Collectors.toMap(CertificateImg::getCertificateType, Function.identity()));
            carInfos.get(i).setPhotoMap(collect1);

        }
        List<CarInfo> collect = carInfos.stream().filter(carInfo -> carInfo.getApprovalStatus().equals(1)).collect(Collectors.toList());
        vo.setReviewedCarInfoList(collect);
        List<CarInfo> collect1 = carInfos.stream().filter(carInfo -> !carInfo.getApprovalStatus().equals(1)).collect(Collectors.toList());
        if(collect1.size()>0){
            vo.setToBeReviewed(collect1.get(0));
        }
        return vo;
    }

    @Override
    public List<CarInfo> getCarInfoByUserIdAndStatus(String userId, Integer approvalStatus) {
        CarInfoListVo vo = new CarInfoListVo();
        QueryWrapper<CarInfo> query = Wrappers.query();
        query.lambda().eq(CarInfo::getUserId, userId);
        query.lambda().eq(CarInfo::getApprovalStatus,approvalStatus);
        query.lambda().eq(CarInfo::getDelFlag, 0);

        List<CarInfo> carInfos = carInfoMapper.selectList(query);
        QueryWrapper<CertificateImg> queryCertificateImg = Wrappers.query();
        queryCertificateImg.lambda().eq(CertificateImg::getUserId, userId);
        List<CertificateImg> certificateImgs = certificateImgMapper.selectList(queryCertificateImg);
        for (int i = 0; i < carInfos.size(); i++) {
            String id = carInfos.get(i).getId();
            List<CertificateImg> collect = certificateImgs.stream().filter(certificateImg -> id.equals(certificateImg.getCarId())).collect(Collectors.toList());
            //把照片封装到车辆钟
            Map<String, CertificateImg> collect1 = collect.stream().collect(Collectors.toMap(CertificateImg::getCertificateType, Function.identity()));
            carInfos.get(i).setPhotoMap(collect1);
        }
        return carInfos;
    }

    @Override
    public List<CarInfo> getCarInfoListByUserId(String userId) {
        CarInfoListVo vo = new CarInfoListVo();
        QueryWrapper<CarInfo> query = Wrappers.query();
        query.lambda().eq(CarInfo::getUserId, userId);
        query.lambda().eq(CarInfo::getApprovalStatus,1);
        query.lambda().eq(CarInfo::getDelFlag, 0);

        List<CarInfo> carInfos = carInfoMapper.selectList(query);
        QueryWrapper<CertificateImg> queryCertificateImg = Wrappers.query();
        queryCertificateImg.lambda().eq(CertificateImg::getUserId, userId);
        List<CertificateImg> certificateImgs = certificateImgMapper.selectList(queryCertificateImg);
        for (int i = 0; i < carInfos.size(); i++) {
            String id = carInfos.get(i).getId();
            List<CertificateImg> collect = certificateImgs.stream().filter(certificateImg -> id.equals(certificateImg.getCarId())).collect(Collectors.toList());
            //把照片封装到车辆钟
            Map<String, CertificateImg> collect1 = collect.stream().collect(Collectors.toMap(CertificateImg::getCertificateType, Function.identity()));
            carInfos.get(i).setPhotoMap(collect1);
        }
        return carInfos;
    }
}
